package com.tyut.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tyut.common.exception.AccountNotFoundException;
import com.tyut.common.exception.PasswordErrorException;
import com.tyut.common.result.PageResult;
import com.tyut.domain.User;
import com.tyut.dto.UserDTO;
import com.tyut.dto.UserLoginDTO;

import com.tyut.dto.UserPageQueryDTO;
import com.tyut.mapper.UserMapper;
import com.tyut.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;


import java.time.LocalDateTime;
import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public User login(UserLoginDTO userLoginDTO) {
        String username = userLoginDTO.getUsername();
        String password = userLoginDTO.getPassword();

        //1.根据用户名查询数据库中的用户信息
        User user = userMapper.getByUsername(username);

        //2.处理登录异常信息
        if (user == null) {
            String msg = "用户名不存在";
            throw new AccountNotFoundException(msg);
        }
        //密码比对
        // TODO 后期需要进行md5加密，然后再进行比对
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        if (!password.equals(user.getPassword())) {
            //密码错误
            String msg = "密码错误";
            throw new PasswordErrorException(msg);
        }

        //3、返回实体对象
        return user;
    }

    /**
     * 添加用户
     * @param userDTO
     */

    public void add(UserDTO userDTO) {
        //1.新建员工对象接收DTO中的数据
        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        //2.设置新用户的登录密码，默认为123456,设置的密码应为默认密码经过MD5加密后的密码
        user.setPassword(DigestUtils.md5DigestAsHex(userDTO.getPassword().getBytes()));
        //3.设置用户创建时间
        user.setCreateTime(LocalDateTime.now());
        userMapper.add(user);

    }


    /**
     * 分页查询普通用户信息
     * @param userPageQueryDTO
     * @return
     */
    public PageResult pageQuery1(UserPageQueryDTO userPageQueryDTO) {
        //开启分页查询
        PageHelper.startPage(userPageQueryDTO.getPage(), userPageQueryDTO.getPageSize());

        Page<User> page = userMapper.pageQuery1(userPageQueryDTO);
        long total = page.getTotal();
        List<User> records = page.getResult();
        return new PageResult(total, records);
    }
    /**
     * 分页查询歌手信息
     * @param userPageQueryDTO
     * @return
     */
    public PageResult pageQuery2(UserPageQueryDTO userPageQueryDTO) {
        //开启分页查询
        PageHelper.startPage(userPageQueryDTO.getPage(), userPageQueryDTO.getPageSize());

        Page<User> page = userMapper.pageQuery2(userPageQueryDTO);
        long total = page.getTotal();
        List<User> records = page.getResult();
        return new PageResult(total, records);
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    @Override
    public User getById(Integer id) {
        return userMapper.getById(id);
    }

    /**
     * 更新用户信息
     * @param userDTO
     */
    @Override
    public void updateUser(UserDTO userDTO) {
        User user = new User();
        BeanUtils.copyProperties(userDTO,user);
        userMapper.update(user);

    }

    /**
     * 启用禁用歌手账号
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, int id) {

            User user = User.builder()
                    .activation(status)
                    .id(id)
                    .build();
            userMapper.update(user);

    }


}
